package com.ddzh;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import java.time.Clock;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

/*
pom.xml
<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-core</artifactId>
  <version>4.5.3</version>
</dependency>

用户登录名称 sms@1982102985911181.onaliyun.com
AccessKey ID LTAI4G3X3orEs83545VQ2UXP
AccessKey Secret 9vtL4wOWYWVPVvCCddfEBOXgMIvmqe

chenggong:SMS_205075131
yanzhengma :SMS_205075124
*/
public class SMSTest {
    public static void main(String[] args) {
//        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
//        IAcsClient client = new DefaultAcsClient(profile);
//
//        CommonRequest request = new CommonRequest();
//        request.setSysMethod(MethodType.POST);
//        request.setSysDomain("dysmsapi.aliyuncs.com");
//        request.setSysVersion("2017-05-25");
//        request.setSysAction("AddShortUrl");
//        request.putQueryParameter("RegionId", "cn-hangzhou");
//        try {
//            CommonResponse response = client.getCommonResponse(request);
//            System.out.println(response.getData());
//        } catch (ServerException e) {
//            e.printStackTrace();
//        } catch (ClientException e) {
//            e.printStackTrace();
//        }
//        System.out.println(generatorCode(4));
//        System.out.println(generatorCode(6));
        getTime();
    }

    private static void getTime(){
        //获取系统当前时间的四种方式：
        System.out.println(Calendar.getInstance().getTimeInMillis());
        System.out.println(System.currentTimeMillis());
        System.out.println(Clock.systemDefaultZone().millis());
        System.out.println(new Date().getTime());

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));

    }
    private static String generatorCode(Integer num){
        String code="";
        //方法二:问题，生成的验证码没有 0 开头的，优点：效率高
        if (num==null || (num!=4 && num!=6)){
            throw new RuntimeException("必须是4位或6位");
        }
        //4位：1000 - 9999
        Random random = new Random();
        if (num==4){
            return String.valueOf(random.nextInt(9000) + 1000);//0-8999 + 1000
        }else{
            return String.valueOf(random.nextInt(900000) + 100000);//0-8999 + 1000
        }

        /*方法1：缺点：用StringBuilder进行字符串拼接，性能低==》可以直接用Integer加，然后再转为String
        Random random = new Random();//如果种子固定，每次产生的随机数顺序是相同的，所以不用种子
        String code="";
        for (Integer i = 0; i < num; i++) {
            code = random.nextInt(10) + code;
        }
        System.out.println("code = " + code);
        return code;*/
    }
}
